package es.acm.calendario.flex
{
	import es.acm.calendario.IDayCalendar;
	
	import mx.states.State;
	
	import spark.components.Button;
	import spark.components.Label;
	import spark.layouts.HorizontalLayout;
	import spark.skins.spark.SkinnableContainerSkin;
	
	[HostComponent("es.acm.calendario.flex.DayCalendar")]
	[States('normal','active','disabled','todaynormal','todayactive','todaydisabled','over')]
	public class DayCalendarSkin extends SkinnableContainerSkin
	{
		public var label:Label;
		public function DayCalendarSkin()
		{
			label = new Label();
			label.styleName = 'dayCalendario';
			super();
		}
		
		override public function initialize():void {
			super.initialize();
			
			super.states.push(new State({name:'normal'}));
			super.states.push(new State({name:'active'}));
			super.states.push(new State({name:'disabled'}));
			super.states.push(new State({name:'todaynormal'}));
			super.states.push(new State({name:'todayactive'}));
			super.states.push(new State({name:'todaydisabled'}));
			super.states.push(new State({name:'over'}));
		}
		
		override protected function createChildren():void {
			super.createChildren();
			
			/*label.setStyle('fontSize', 14);
			label.setStyle('textAlign','center');*/
			contentGroup.addElement(label);
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			label.width = unscaledWidth;
			label.left = unscaledWidth/2 - label.width/2;
			label.top = unscaledHeight/2 - label.height/3;
			
			// Draw the background
			paintDay();
		}
		
		override public function set currentState(value:String):void {
			super.currentState = value;
			
			// Draw the background
			paintDay();
		}
		
		private function paintDay():void {
			var lbColor:uint;
			var lbFontWeight:String;
			var bgColor:uint;
			
			switch(currentState)
			{
				case 'normal':
					lbColor = 0x000000;
					bgColor = 0xffffff;
					lbFontWeight = 'normal';
					break;
				case 'active':
					lbColor = 0x000000;
					bgColor = 0xB8D8F5;
					lbFontWeight = 'bold';
					break;
				case 'disabled':
					lbColor = 0xEEEEEE;
					bgColor = 0xCCCCCC;
					lbFontWeight = 'normal';
					break;
				case 'todaynormal':
					lbColor = 0xffffff;
					bgColor = 0xff0000;
					lbFontWeight = 'normal';
					break;
				case 'todayactive':
					lbColor = 0xffffff;
					bgColor = 0xff0000;
					lbFontWeight = 'bold';
					break;
				case 'todaydisabled':
					lbColor = 0xffffff;
					bgColor = 0xff0000;
					lbFontWeight = 'bold';
					break;
				case 'over':
					lbColor = 0xff55ff;
					bgColor = 0xdddddd;
					lbFontWeight = 'normal';
					break;
			}
			
			label.setStyle('color', lbColor);
			label.setStyle('fontWeight', lbFontWeight);
			graphics.clear();
			
			if (hostComponent) {
				var line:int=1;
				var lineColor:uint= 0x000000;
				var pad1:int=0;
				var pad2:int=0;
				if (DayCalendar(hostComponent).isSelected) {
					line = 3;
					pad1 = 1;
					pad2 = 2
					lineColor = 0x333333;
				}
				graphics.lineStyle(line, lineColor, alpha, true);
			}
			
			graphics.beginFill(bgColor, 1.0);
			graphics.drawRect(0 + pad1, 0 + pad1, unscaledWidth - pad2, unscaledHeight - pad2);
			graphics.endFill();
		}
	}
}